package com.qf.sportsevent.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v4.util.Pair;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/12/20.
 */

public class SubscribeRecyclerAdapter<D> extends RecyclerView.Adapter<SubscribeRecyclerAdapter.ViewHolder>{
    private List<D> list;
    private Context mContext;
    private Map<Integer,Pair<Integer,Integer>> map;
    private LayoutInflater inflater;

    public SubscribeRecyclerAdapter( Context mContext,List<D> list, Map<Integer, Pair<Integer, Integer>> map) {
        this.mContext = mContext;
        this.list = list;
        this.map = map;
        inflater=LayoutInflater.from(mContext);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType!=-1){
            return new ViewHolder(DataBindingUtil.inflate(inflater,viewType,parent,false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        D d = list.get(position);
        Pair<Integer, Integer> pair = getPair(d);
        if (pair != null) {
            holder.binding.setVariable(pair.second,d);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public int getItemViewType(int position) {
        D d = list.get(position);
        Pair<Integer, Integer> pair = getPair(d);
        if (pair != null) {
            return pair.first;
        }
        return -1;
    }

    private Pair<Integer,Integer> getPair(D d){
        if(d instanceof String){
            return map.get(12);
        }else if(d instanceof ArrayList<?>){
            return map.get(11);
        }
        return null;
    }

    public void add(D d){
        if(list!=null&&d!=null){
            add(list.size(),d);
        }
    }

    public void add(int position,D d){
        if(list!=null&&d!=null){
            list.add(position,d);
            notifyItemInserted(position);
        }
    }

    public void addAll(Collection<? extends D> collection){
        if(list!=null&&collection!=null){
            addAll(list.size(),collection);
        }
    }

    public void remove(D d){
        if (list != null && d != null && list.contains(d)) {
            remove(list.indexOf(d));
        }
    }

    public void remove(int position){
        if(list !=null && list.size() > position){
            list.remove(position);
            notifyItemRemoved(position);
        }
    }

    public List<D> getData(){
        return list;
    }

    public void addAll(int position,Collection<? extends D> collection){
        if(list!=null&&collection!=null){
            list.addAll(position,collection);
            notifyItemRangeInserted(position,collection.size());
        }
    }


    public static class ViewHolder extends RecyclerView.ViewHolder{
        private ViewDataBinding binding;

        public ViewHolder(ViewDataBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
